#include "device_manage.h"
#include "fun_flash.h"
#include <process.h>
#include <etimer.h>
#include <string.h>
#include "uart.h"
#include "74hc595.h"
#include "time.h"
#include "stm32f1xx_hal.h"   
#include "flash.h"


uint32_t flash_write_addr = 0;		//从机信息记录地址
uint32_t flash_write_str_addr = 0;	//从机信息记录开始地址
uint32_t flash_write_end_addr = 0;	//从机信息记录结束地址
uint32_t flash_del_addr = 0;		//要删除的地址
uint32_t flash_mod_addr = 0;		//要修改的地址
uint32_t flash_cur_addr = 0;		//当前从机地址


void get_sflash_addr(void)
{
	sFlashPack  FlashPack;
	flash_write_addr = 0;
	
	if(cfg_data.Val.Flash_index == 0){
		flash_write_str_addr = FLASH_START_ADDRES0;
		flash_write_end_addr = FLASH_END_ADDRES0;
	}else{
		flash_write_str_addr = FLASH_START_ADDRES1;
		flash_write_end_addr = FLASH_END_ADDRES1;
	}
	for(flash_write_addr = flash_write_str_addr ; flash_write_addr < flash_write_end_addr; flash_write_addr+=16){
		ReadFlash(flash_write_addr,0,15,FlashPack.Var);
		if(FlashPack.Val.valid != flash_valid){
			break;
		}
	}
}

bool check_flashnum(iSMacSn SalveMac)
{
	sFlashPack  Flash_Pack;
	//在flash中查找该从机mac
	bool is_exist = false;			//从机是否存在标志
	for(uint32_t i = flash_write_str_addr; i<flash_write_end_addr; i=i+16){
		ReadFlash(i,0,15,Flash_Pack.Var);
		if(Flash_Pack.Val.valid == flash_valid){
			if(	Flash_Pack.Val.mac.temp0 == SalveMac.temp0 &&
				Flash_Pack.Val.mac.temp1 == SalveMac.temp1 &&
				Flash_Pack.Val.mac.temp2 == SalveMac.temp2 ){
				is_exist = true;
				flash_cur_addr = i;
				break;
			}
		}else{
			break;
		}
	}
	return is_exist;
}

void add_flashnum(iSMacSn SalveMac, uint16_t add_num)
{
	sFlashPack  Flash_Pack;
	
	Flash_Pack.Val.mac.temp0 = SalveMac.temp0;
	Flash_Pack.Val.mac.temp1 = SalveMac.temp1;
	Flash_Pack.Val.mac.temp2 = SalveMac.temp2;
	Flash_Pack.Val.slave.Val = add_num;						//获取mac对应的号码
	Flash_Pack.Val.valid = flash_valid;
	WriteFlash(flash_write_addr,0,15,&Flash_Pack.Var[0]);
	flash_write_addr += 16;									//flash地址
}


uint16_t get_curflash_num()
{
	sFlashPack  Flash_Pack;
	ReadFlash(flash_cur_addr,0,15,Flash_Pack.Var);
	return Flash_Pack.Val.slave.Val;
}

void mod_flashnum(uint16_t num)
{
	uint32_t begin_r = 0;
	uint32_t end_r = 0;
	uint32_t begin_w = 0;
	uint32_t end_w = 0;
	sFlashPack  FlashPack;
	
	flash_mod_addr = flash_cur_addr;
	
	if(cfg_data.Val.Flash_index == 0){	//当前是0区，切换到1区
		begin_w = FLASH_START_ADDRES1;
		end_w = FLASH_END_ADDRES1;
		begin_r = FLASH_START_ADDRES0;
		end_r = FLASH_END_ADDRES0;
		cfg_data.Val.Flash_index = 1;
	}else{
		begin_w = FLASH_START_ADDRES0;
		end_w = FLASH_END_ADDRES0;
		begin_r = FLASH_START_ADDRES1;
		end_r = FLASH_END_ADDRES1;
		cfg_data.Val.Flash_index = 0;
	}
	uint8_t data[15] = {0};
	WriteFlash(begin_w,0,15,data);
	flash_write_str_addr = begin_w;
	flash_write_end_addr = end_w;
	for( ; begin_r < end_r; begin_r=begin_r+16){
		ReadFlash(begin_r,0,15,FlashPack.Var);
		if(FlashPack.Val.valid != flash_valid){			//当读到无效，退出
			break;
		}
		if(flash_mod_addr == begin_r){					//当前地址为修改地址时，修改号码
			FlashPack.Val.slave.Val = num;
		}
		if(begin_w<end_w){
			WriteFlash(begin_w,0,15,FlashPack.Var);
		}
		begin_w += 16;
	}
	flash_write_addr = begin_w;
	write_cfg(&cfg_data);			//记录配置
}

void del_flashnum()
{
	uint32_t begin_r = 0;
	uint32_t end_r = 0;
	uint32_t begin_w = 0;
	uint32_t end_w = 0;
	sFlashPack  FlashPack;
	
	flash_del_addr = flash_cur_addr;
	
	if(cfg_data.Val.Flash_index == 0){	//当前是0区，切换到1区
		begin_w = FLASH_START_ADDRES1;
		end_w = FLASH_END_ADDRES1;
		begin_r = FLASH_START_ADDRES0;
		end_r = FLASH_END_ADDRES0;
		cfg_data.Val.Flash_index = 1;
	}else{
		begin_w = FLASH_START_ADDRES0;
		end_w = FLASH_END_ADDRES0;
		begin_r = FLASH_START_ADDRES1;
		end_r = FLASH_END_ADDRES1;
		cfg_data.Val.Flash_index = 0;
	}
	uint8_t data[15] = {0};
	WriteFlash(begin_w,0,15,data);
	flash_write_str_addr = begin_w;
	flash_write_end_addr = end_w;
	for( ; begin_r < end_r; begin_r=begin_r+16){
		if(flash_del_addr == begin_r){					//当前地址为删除地址时，不记录
			continue;
		}
		ReadFlash(begin_r,0,15,FlashPack.Var);
		if(FlashPack.Val.valid != flash_valid){			//当读到无效，退出
			break;
		}
		if(begin_w<end_w){
			WriteFlash(begin_w,0,15,FlashPack.Var);
		}
		begin_w += 16;
	}
	flash_write_addr = begin_w;
	write_cfg(&cfg_data);			//记录配置
}

void del_allflashnum()
{
	EraseFlash(FLASH_START_ADDRES0,FLASH_START_ADDRES0+0x400);
	EraseFlash(FLASH_START_ADDRES1,FLASH_START_ADDRES1+0x400);
	get_sflash_addr();
}

